package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.XYAnchor;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPolicy;

import edu.pku.sei.modeler.edit.policies.LinkBendpointEditPolicy;
import edu.pku.sei.modeler.editpolicies.TimeLineBendPointPolicy;
import edu.pku.sei.modeler.model.core.MetaElement;
import edu.pku.sei.modeler.model.structure.MetaBendPoint;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;

public class TimeLineEditpart extends UMLLinkEditpart {

	public TimeLineEditpart(MetaShapeElement conn) {
		super(conn);
	}
	
	@Override
	public void activate() {
		super.activate();
		MetaNode source = getLink().getSource();
		if (source != null) {			
			source.addPropertyChangeListener(modelListener);
			MetaElement parentDomainModel = source.getModelElement();
			if (parentDomainModel != null) {
				parentDomainModel.addPropertyChangeListener(modelListener);
			}
		}
	}
	@Override
	public void deactivate() {
		super.deactivate();
		
		MetaNode source = getLink().getSource();
		if (source != null) {			
			source.removePropertyChangeListener(modelListener);
			MetaElement parentDomainModel = source.getModelElement();
			if (parentDomainModel != null) {
				parentDomainModel.removePropertyChangeListener(modelListener);
			}
		}
	}
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE, new TimeLineBendPointPolicy((MetaLink) getModel()));
	}
	@Override
	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();
		conn.setBackgroundColor(ColorConstants.blue);
		return conn;
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName(); 
		if (StructurePackage.NODE_LOCATIONX.equals(propertyName)) {
			int offset = ((Integer)event.getNewValue()).intValue() - ((Integer)event.getOldValue()).intValue();
			Point move = new Point(offset, 0);
			refreshBendPoints(move);
			
			XYAnchor anchor = (XYAnchor) getTargetConnectionAnchor();
			anchor.getLocation(null).translate(move);
			getConnectionFigure().setTargetAnchor(anchor);
			
			anchor = (XYAnchor) getSourceConnectionAnchor();
			anchor.getLocation(null).translate(move);
			getConnectionFigure().setSourceAnchor(anchor);
			
			
		} else if (StructurePackage.NODE_LOCATIONY.equals(propertyName)) {
			int offset =((Integer)event.getNewValue()).intValue()  - ((Integer)event.getOldValue()).intValue();
			Point move = new Point(0, offset);
			refreshBendPoints(move);
			XYAnchor anchor = (XYAnchor) getTargetConnectionAnchor();
			anchor.getLocation(null).translate(move);
			getConnectionFigure().setTargetAnchor(anchor);
			
			anchor = (XYAnchor) getSourceConnectionAnchor();
			anchor.getLocation(null).translate(move);
			getConnectionFigure().setSourceAnchor(anchor);
		}
		super.handlePropertyChanged(event);
	}
	
	public void refreshBendPoints(Point move) {
		if (getFigure() instanceof Connection) {
			List bendPoints = getLink().getBendPoints();
			for (Iterator iterator = bendPoints.iterator(); iterator.hasNext();) {
				MetaBendPoint bp = (MetaBendPoint) iterator.next();
				bp.x = bp.x + move.x;
				bp.y = bp.y + move.y;
				
			}
		}
		refreshVisuals();
	}

}
